﻿/*!	@file	r_draw_pixelShader_dx11.cpp
	@brief	フラグメントシェーダ
Copyright (c) 2010 Yuya Yamaguchi

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#include "r_draw_pixelShader_dx11.h"
#if R_TARGET_UNIQUEIS(R_TARGET_UNIQUE_WINDX11)
#include "../../r_debug.h"
#include <d3dx11.h>
#include "r_draw_core_dx11.h"

#include "../../util/r_util_memory_zero.h"
#include "../../util/r_util_memory_copy.h"
#include "../../util/r_util_com_release.h"
#include "../../util/r_util_str.h"

// >>>>>>>>>> RTTI >>>>>>>>>>
#include "../../rtti/r_rtti_reflection_implement.h"
namespace r{
	namespace draw{
		namespace dx11{
RRTTI_REFLECTION_IMPLEMENT ( r::draw::dx11::PixelShaderDX11, r::draw::PixelShader )

// <<<<<<<<<< RTTI <<<<<<<<<<

			PixelShaderDX11 :: PixelShaderDX11()
				:
				shader ( NULL )
			{
			}

			PixelShaderDX11 :: PixelShaderDX11 ( void* buffer, r_size buffSize, const r_cstr entryFuncName, const r_cstr fileName )
			{
				const r_size	funcNameLength = 512;
				const r_size	fileNameLength = 2048;
				DWORD			shaderCompileFlags = D3D10_SHADER_ENABLE_STRICTNESS;
				ID3D10Blob*		PSBuffer = NULL;
				ID3D10Blob*		ErrorMsg = NULL;
				r_mchar			tmpFuncName[funcNameLength];
				r_mchar			tmpFileName[fileNameLength];
				fRStrCopyToMulti ( tmpFuncName, entryFuncName, funcNameLength, r_tm_strlen ( entryFuncName ) + 1 );
				fRStrCopyToMulti ( tmpFileName, fileName, funcNameLength, r_tm_strlen ( fileName ) + 1 );
				const r_mstr	profileStr = NULL;
				switch ( r::rtti::dynamicCast<CoreDX11*> ( Core::getInstance() )->getFeatureLevel() ) {
				case D3D_FEATURE_LEVEL_9_1:
					profileStr = "ps_4_0_level_9_1";
					break;
				case D3D_FEATURE_LEVEL_9_2:
					profileStr = "ps_4_0_level_9_2";
					break;
				case D3D_FEATURE_LEVEL_9_3:
					profileStr = "ps_4_0_level_9_3";
					break;
				case D3D_FEATURE_LEVEL_10_0:
					profileStr = "ps_4_0";
					break;
				case D3D_FEATURE_LEVEL_10_1:
					profileStr = "ps_4_1";
					break;
				case D3D_FEATURE_LEVEL_11_0:
					profileStr = "ps_5_0";
					break;
				}
				HRESULT	hr = D3DX11CompileFromMemory (
								 reinterpret_cast<r_mstr> ( buffer ),
								 buffSize,
								 tmpFileName,
								 NULL,
								 NULL,
								 tmpFuncName,
								 profileStr,
								 shaderCompileFlags,
								 0,
								 NULL,
								 &PSBuffer,
								 &ErrorMsg,
								 NULL );
				if ( FAILED ( hr ) ) {
					// どぉしてシングルバイトなの?!
					RDEBUG_WARRN_MULTI ( "%s\n", reinterpret_cast<r_mstr> ( ErrorMsg->GetBufferPointer() ) );
				}
				hr = r::rtti::dynamicCast<CoreDX11*> ( Core::getInstance() )->getD3DDevice()->CreatePixelShader ( PSBuffer->GetBufferPointer(), PSBuffer->GetBufferSize(), NULL, &shader );
				if ( FAILED ( hr ) ) {
					RDEBUG_WARRN ( _RST ( "failed create vertexShader %08x\n" ), hr );
				}
				r_tm_comRelease ( PSBuffer );
				r_tm_comRelease ( ErrorMsg );
			}

			PixelShaderDX11 :: ~PixelShaderDX11()
			{

			}

			ID3D11PixelShader*	PixelShaderDX11 :: getShader() const
			{
				return shader;
			}
		}
	}
}

#endif // R_TARGET_UNIQUEIS(R_TARGET_UNIQUE_WINDX11)
